#include <iostream>

using namespace std;
const int N = 10010,M = 1e7 + 10;
typedef long long LL;
int w[N], t[N];
LL f[M];
int main()
{
	int n, m; cin >> m >> n;


	for (int i = 1; i <= n; i++) cin >> t[i] >> w[i];

	for (int i = 1; i <= n; i++)
	{
		for (int j = t[i]; j <= m; j++)
		{
			f[j] = max(f[j], f[j - t[i]] + w[i]);
		}
	}
	cout << f[m] << endl;
	return 0;
}